(() => {
	let ele = {};
	ele.son_num = $('#son_num');
	ele.num_bot_n = $('#num_bot_n');
	ele.prizes = $('#prizes');
    ele.time = $('#time');
	ele.explainlsit = $('#explainlsit');
	ele.btn = $('#btn');

	ele.modal_getsuccess = $('#modal-getsuccess'); 
	ele.close_getsuccess = $('#close-getsuccess');
	ele.getsuccessList = $('#getsuccessList');





    ele.time.innerHTML = getday()

	getlist()

	// 获取中奖记录接口
	function getlist() {
		let token = getCookie("token");
		ajax({
			url: API_PATH.WINNINGLOG,
			method: "GET",
			header: {
				Authorization: token,
			},
			success: (res) => {
				if (res.code == 200) {
					if(res.data.length != 0){
						ele.is_receive = res.data.is_receive
						drawinfo(res)
						myselect(res)
						drawprize(res)
						drawexplain(res)
						drawreward(res.data.pending_rewards);
						ele.btn.onclick = ()=>{
							if(ele.is_receive == 1) {
								openTips('已领取')
							}else if(res.data.user_lucky_list == '' || !res.data.user_lucky_list){
								openTips('无可领取奖励')
							}else if(res.data.pending_rewards == '' || res.data.pending_rewards == null){
								openTips('无可领取奖励')
							} else{
								let token = getCookie("token");
								ajax({
									url: API_PATH.LUCKNUMINFOME,
									method: "GET",
									header: {
										Authorization: token,
									},
									success: (res) => {
										if (res.code == 200) {
											openMdl(ele.modal_getsuccess);
											getlist()
										} else  openTips(res.message || "未知错误");
									},
								});
							}
						}
					}else {
						ele.btn.onclick = ()=>{
							openTips('无可领取奖励')
						}
					}
				} else openTips(res.msg || "未知错误");
			},
		});
	}

	

	
	// 渲染中奖号码
	function drawinfo(res) {
		console.log(res);
			let html = res.data.last_res.lucky_num.map((item, index) => {
				return `<div class="son_right_img">
							<span>${item}</span>
						</div>`
			}).join('')
			ele.son_num.innerHTML = html
	}

	// 渲染领取奖励弹窗
	function drawreward(data){
		let html = data.map((item,index)=>{
			return `<div class="debox">
						<div class="wups">
							<img src=" ${IMG_DOMAIN}${item.reward[0]
								.logo}" alt="">
						</div>
						<p class="enums">${item.reward[0].num}</p>
					</div>`
		}).join('');

		ele.getsuccessList.innerHTML = html;
	}


	// 渲染所选号码
	function myselect(res) {
		let html = res.data.user_lucky_list.map((item, index) => {
			let indextext = index == 0 ? '一' : index == 1 ? '二' : index == 2 ? '三' : index == 3 ? '四' :
				index == 4 ? '五' : ''

			let html2 = item.chose_num.map((v, i) => {
				return `<li>
                        <img src="http://img.qxgs.cc/tiantianlaishouzu/img/number/yuan1.png" alt="" />
                        <span>${v.num}</span>
                    </li>`
			}).join('');

			return `<li class="li_bot_n">
                    <div class="left">第${indextext}组</div>
                    <ul class="right">
                        ${html2}
                    </ul>
                </li>`
		}).join('')
		ele.num_bot_n.innerHTML = html
	}

	// 渲染我中奖的描述
	function drawexplain(res){
		let html = res.data.pending_rewards.map((item , index)=>{

			let html2 = item.reward.map((v,i)=>{
				return `<div class="tit_box">
							<span>获得${v.name}</span><img class="yingbi" src="${IMG_DOMAIN}${v.logo}" alt="" />
							<span>×${v.num}</span>
						</div>`
			}).join('')
			return `<div class="tit_top">
						<span>恭喜你中了${item.several_prizes}等奖</span>
						${html2}
					</div>`
		}).join('')
		ele.explainlsit.innerHTML = html
	}	

	// 渲染奖项

	function drawprize(res) {
		let html = res.data.prizes.map((item, index) => {
			let indextext = index == 0 ? '一' : index == 1 ? '二' : index == 2 ? '三' : index == 3 ? '四' :
				index == 4 ? '五' : ''
			let prizesindex = index == 0 ? '5' : index == 1 ? '4' : index == 2 ? '3' : index == 3 ? '2' :
				index == 4 ? '1' : ''
			return `<li class="charts_li">
                        <div>${indextext}等奖</div>
                        <div>${item.num}</div>
                        <div>中${prizesindex}个号码</div>
                    </li>`
		}).join('');
		ele.prizes.innerHTML = html
	}

    function getday(){
        var today = new Date();
        var yesterday = new Date(today);
        yesterday.setDate(today.getDate() - 1);
        var dd = yesterday.getDate();
        var mm = yesterday.getMonth() + 1;
        var yyyy = yesterday.getFullYear();
        if (dd < 10) {
        dd = '0' + dd;
        }
        if (mm < 10) {
        mm = '0' + mm;
        }
        
        return yyyy + '-' + mm + '-' + dd;


    }


	
	ele.close_getsuccess.onclick = ()=>{
		closeMdl(ele.modal_getsuccess)
	}


})();